package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public List<ProductModel> findAll(ProductModel productModel) {
        String sql = "select * from product where 1=1 ";
        if (!"".equals(productModel.getName()) && productModel.getName() != null) {
            sql += " and name like '%" + productModel.getName() + "%'";
        }
        sql += " order by id desc " +
                " limit " + (productModel.getPage() - 1) * productModel.getLimit() + "," + productModel.getLimit() + "";
        return setList(sql);
    }

    @Override
    public ProductModel findById(int id) {
        String sql = "select * from product where id=" + id;
        List<ProductModel> list = setList(sql);
        int count = list.size();
        return count > 0 ? list.get(0) : null;
    }

    @Override
    public int changeEnable(int id) {
        return 0;
    }

    @Override
    public int getCount(ProductModel productModel) {
        String sql = "select count(*) count from product where 1=1 ";
        if (!"".equals(productModel.getName()) && productModel.getName() != null) {
            sql += " and name like '%" + productModel.getName() + "%'";
        }
        sql += " order by id desc ";
        ResultSet resultSet = JDBC.query(sql);
        try {
            while (resultSet.next()) {
                return resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int add(ProductModel productModel) {
        return 0;
    }

    @Override
    public int update(ProductModel productModel) {
        String sql = "UPDATE `product` \n" +
                "SET `name` = '" + productModel.getName() + "', \n" +
                "`normal_price` = '1', \n" +
                "`price` = '1', \n" +
                "`content` = '1', \n" +
                "`is_show` = '1', \n" +
                "`is_recommend` = '1',\n";
        if (productModel.getImgHref() != null && !"".equals(productModel.getImgHref())) {
            sql += " `img_href` = '" + productModel.getImgHref() + "',\n";
        }
        sql += " `update_time` = now(),\n";
        sql += " `where_show` = '1' ,\n";
        if (productModel.getEnable() != -1) {
            sql += " `enable` = '1',\n";
        }


        sql += " remark=''  WHERE (`id` = '72');";
        return JDBC.update(sql);
    }

    @Override
    public int delete(ProductModel productModel) {
        String sql = "delete from product where id=" + productModel.getId();
        return JDBC.update(sql);
    }

    private List<ProductModel> setList(String sql) {
        ResultSet resultSet = JDBC.query(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel model = new ProductModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setCreateTime(resultSet.getString("create_time"));
                model.setNormalPrice(resultSet.getFloat("normal_price"));
                model.setEnable(resultSet.getInt("enable"));
                model.setPrice(resultSet.getDouble("price"));
                model.setContent(resultSet.getString("content"));
                model.setImgHref(resultSet.getString("img_href"));
                model.setIsShow(resultSet.getInt("is_show"));
                model.setIsRecommend(resultSet.getInt("is_recommend"));
                model.setCuisineId(resultSet.getInt("cuisine_id"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
//            JDBC.close();
        }
        return list;
    }
}
